//
//  LoginViewController.m
//  pushNewsManager
//
//  Created by Jobs on 7/25/17.
//  Copyright © 2017 com.Iori. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginApi.h"
#import "Header.h"
#import "AdminEntity.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString* strMobile = [[NSUserDefaults standardUserDefaults] stringForKey:MOBILEKEY];
    NSString* strPwd = [[NSUserDefaults standardUserDefaults] stringForKey:PASSWORDKEY];
    if(strMobile && strPwd)
    {
        self.txtPhone.text = strMobile;
        self.txtPwd.text = strPwd;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)btnLogin_click:(UIButton *)sender
{
    LoginApi *api = [LoginApi new];
    api.name = [self.txtPhone.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    api.password = [self.txtPwd.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    WS(weakSelf);
    [MessageBox displayLoadingInView:weakSelf.view];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        [MessageBox removeLoading];
        if([IoriJsonHelper getErrorCode:request] == 100)
        {
            AdminEntity *entity = [AdminEntity yy_modelWithDictionary:[IoriJsonHelper getDictForKey:@"admin" fromDict:request.responseJSONObject]];
            [UserInfo sharedUser].admin = entity;
            [[NSUserDefaults standardUserDefaults] setObject:api.name forKey:MOBILEKEY];
            [[NSUserDefaults standardUserDefaults] setObject:api.password forKey:PASSWORDKEY];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccessNotification" object:nil];
            [self.navigationController popViewControllerAnimated:NO];
        }
        else
        {
            [weakSelf showMessage:@"登录失败"];
        }
    } failure:^(YTKBaseRequest *request) {
        [weakSelf showMessage:@"网络错误，登录失败"];
        [MessageBox removeLoading];
    }];
    
}
@end
